package simple.demo;

import java.util.concurrent.TimeUnit;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.broker.BrokerFactory;
import org.apache.activemq.broker.BrokerService;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.PosixParser;

public class Client {

	private BrokerService broker;
	private ConnectionFactory connFactory;
	private Connection conn;

	public void run(String [] args) throws Exception {
		Options options = Main.makeOptions();
		
		CommandLineParser parser = new PosixParser();
		CommandLine cmd = parser.parse(options, args);

		initBroker();
		
		initConnection();
		
		Session session = conn.createSession(true, Session.AUTO_ACKNOWLEDGE);
		MessageProducer producer = session.createProducer(session.createQueue("T"));
		
		for(int k=0; k<1000 * 1000; ++k) {
			producer.send(session.createObjectMessage(new byte[1024])); //1K size, 1G in total
			if(k % 100 == 0)
				TimeUnit.MILLISECONDS.sleep(10);
			if(k % 10000 == 0)
				System.out.println(k);
		}
		
		TimeUnit.MINUTES.sleep(60); 
	}

	void initConnection() throws JMSException {
		connFactory = new ActiveMQConnectionFactory("vm://x?create=false");
		conn = connFactory.createConnection();
		conn.start();
	}

	void initBroker() throws Exception {
		broker = BrokerFactory.createBroker("xbean:client.xml?validate=false");
		broker.start();
	}
	
	void shutdown() throws Exception {
		broker.stop();
	}

	
}
